package com.dongdongsongcan.app.activity;

import android.content.Context;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.dongdongsongcan.app.R;
import com.dongdongsongcan.app.SMSReceiver;
import com.dongdongsongcan.app.control.UserControl;
import com.dongdongsongcan.app.control.bean.AuthnumData;
import com.dongdongsongcan.app.control.bean.UserData;
import com.su.dolphin.control.task.CtrlTask.IControlResponse;
import com.su.dolphin.core.AppThread;
import com.su.dolphin.utils.mgr.DpActivityManager;
import com.su.dolphin.utils.ui.TipsUtil;
import com.su.dolphin.view.activity.BaseActivity;

//已经不需要注册了!!
public class RegisterActivity extends BaseActivity {

    private CheckBox mRegister_agreement;

    public static void startFrom(Context context) {
        DpActivityManager.i().startActivity(context, RegisterActivity.class);
    }

    private EditText mRegister_et_phonenum;
    private Button mRegister_btn_verigycode;
    private EditText mRegister_et_verigycode;
    private Button mRegister_btn_rigister;
    private String mUser_mobile;
    private AuthnumData mAuthnumData;


    private SMSReceiver mSmsReceiver = new SMSReceiver(new SMSReceiver.ISMSListener() {
        @Override
        public void onSmsReceive(String verifyCode) {
            if (mRegister_et_verigycode == null) return;
            mRegister_et_verigycode.setText(verifyCode);
        }
    });


    @Override
    public void onStart() {
        registerReceiver(mSmsReceiver, new IntentFilter(SMSReceiver.SMS_RECEIVED_ACTION));
        super.onStart();
    }

    @Override
    public void onStop() {
        unregisterReceiver(mSmsReceiver);
        super.onStop();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_register);
        super.onCreate(savedInstanceState);
        setTitle("注册");
//        getDpTitleBar().setRightButtonText("用户登陆",
//                new OnRightButtonClickListener() {
//
//                    @Override
//                    public void onRightBtnClick(View button) {
//                        LoginActivity.startFrom(RegisterActivity.this);
//                    }
//                }, null, null);

    }

    @Override
    public void initView() {
        mRegister_et_verigycode = (EditText) findViewById(R.id.register_et_verigycode);
        mRegister_et_phonenum = (EditText) findViewById(R.id.register_et_phonenum);
        mRegister_btn_verigycode = (Button) findViewById(R.id.register_btn_verigycode);
        mRegister_btn_rigister = (Button) findViewById(R.id.register_btn_rigister);
        mRegister_agreement = (CheckBox) findViewById(R.id.register_agreement);
    }

    @Override
    public void setListener() {
        mRegister_btn_verigycode.setOnClickListener(this);
        mRegister_btn_rigister.setOnClickListener(this);
    }

    int tineout = 60;

    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
            case R.id.register_btn_verigycode:
                mRegister_btn_verigycode.setText(tineout + "秒");
                mRegister_btn_verigycode.setEnabled(false);
                AppThread.uiPost(new Runnable() {
                    @Override
                    public void run() {
                        tineout--;
                        mRegister_btn_verigycode.setText(tineout + "秒");
                        if (tineout <= 0) {
                            mRegister_btn_verigycode.setEnabled(true);
                            mRegister_btn_verigycode.setText("重新获取");
                            tineout = 60;
                        } else {
                            AppThread.uiPost(this, 1000);
                        }
                    }
                }, 1000);
                mUser_mobile = mRegister_et_phonenum.getText().toString().trim();
                UserControl.i().authnum(mUser_mobile,
                        new IControlResponse<AuthnumData>() {

                            @Override
                            public void onControlResponse(AuthnumData data) {
                                TipsUtil.toast(
                                        data.getMsg() + data.getAuthnum());
                                if (data.getError() == 0) {
                                    mAuthnumData = data;
                                }
                            }
                        });
                break;
            case R.id.register_btn_rigister:
                if (!mRegister_agreement.isChecked()) {
                    return;
                }
                if (mAuthnumData != null) {
                    String verifyCode = mRegister_et_verigycode.getText()
                            .toString().trim();
                    if (!TextUtils.equals(verifyCode, mAuthnumData.getAuthnum())) {// 这里有大问题,
                        // 可能导致批量注册用户!
                        TipsUtil.toast("验证码错误!");
                        return;
                    }

                    UserControl.i().register_mobile(
                            mUser_mobile, // 这里可以统一为一个接口!
                            mAuthnumData.getAuthnum(),
                            mAuthnumData.getSession_id(),
                            new IControlResponse<UserData>() {

                                @Override
                                public void onControlResponse(UserData data) {
                                    if (data.getError() == 0) {
                                        UserControl.i().saveUser(data.getResult());
                                        MainActivity.startFrom(RegisterActivity.this);
                                    }
                                    TipsUtil.toast(data.getMsg());

                                }
                            });
                } else {
                    TipsUtil.toast("请先获取验证码");
                }

                break;
            default:
                break;
        }
    }

    @Override
    public void initData() {
    }

}
